﻿/*
 * This is a .NET port of the Apache Chemistry QueryParser. http://chemistry.apache.org/
 * The conversion was performed by Brügmann Software GmbH. Programmer: Björn Kremer.
 *
 * This project is still under the original Apache License: 
 * 
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QueryParser
{
   public class StringUtil
   {

      /// <summary>
      /// remove all escape sequences in a string and return unescaped string
      /// escape character is backslash \, so \\ --> \, \' --> ' additional
      /// escaped characters can be allowed in escapedChars </summary>
      /// <param name="literal">
      ///      String to unescape </param>
      /// <param name="escapedChars">
      ///      set of allowed characters to be escaped with a backslash, if set to
      ///      null then ' (quote) and \ (backslash) are allowed to be escaped
      /// @return
      ///      unescaped literal or null if the literal is illegal </param>
      public static string unescape(string literal, string escapedChars)
      {
         char c = '?';
         int i = 0;
         StringBuilder sb = new StringBuilder();

         if (null == escapedChars)
         {
            escapedChars = "\\'";
         }

         if (null == literal)
         {
            return null;
         }

         int len = literal.Length;

         if (len == 1 && literal[0] == '\\')
         {
            return null;
         }

         if (len > 1 && literal[len - 2] != '\\' && literal[len - 1] == '\\')
         {
            return null;
         }

         for (i = 0; i < len; i++)
         {
            c = literal[i];
            if (c == '\\')
            {
               char escChar = literal[i + 1];
               bool matched = false;
               for (int j = 0; j < escapedChars.Length; j++)
               {
                  if (escChar == escapedChars[j])
                  {
                     sb.Append(escChar);
                     ++i;
                     matched = true;
                     break;
                  }
               }

               if (!matched)
               {
                  return null;
               }

            }
            else
            {
               sb.Append(literal[i]);
            }
         }

         return sb.ToString();
      }

   }
}
